const { form, layer } = layui;

$(function () {

    submitForm();

    // 定义密码表单的校验规则
    form.verify({
        pass: [
            /^[\S]{6,12}$/
            , '密码必须6到12位，且不能出现空格'
        ],
        repass: function (value) {
            if (value !== $('#repass').val()) {
                return "两次输入的新密码不一致";
            }
        },
        somePwd: function (value) {
            if (value === $('input[name="oldPwd"]').val()) {
                return '新旧密码不能相同';
            }
        }
    })
})

// 提交表单
function submitForm() {
    form.on('submit(subPwdForm)', data => {
        const { oldPwd, newPwd } = data.field;
        $.post('/my/updatepwd', { newPwd, oldPwd }, result => {
            const { status, message } = result;
            if (status === 0) {
                layer.alert('密码更新成功，请重新登录', function (index) {
                    localStorage.removeItem('token');
                    window.parent.location.href = "/login.html";
                    layer.close(index);
                });
            } else {
                layer.msg(message);
            }
        })
        return false;
    })
}